<template>
  <a-modal
    title="操作"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
  >


    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <!--companyId-->

        <a-form-item :label-col="labelCol" :wrapper-col="wrapperCol" label="所属公司">
          <a-select
            :disabled="isDisabled"
            @change="handleProvinceChange"
            v-decorator="[ 'companyId', {rules: [{ required: true, message: '请选择所属公司' }], initialValue: defaultCompany}]"
            placeholder="请选择所属公司" >
            <a-select-option :value="company.id" v-for="company in companyList" :key="company.id">
              {{company.name}}
            </a-select-option>
          </a-select>
        </a-form-item>

        <!--categoryId 类别-->
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="类别">
          <a-select placeholder="请选择类别" v-decorator="[ 'categoryId', {rules: [{ required: true, message: '请选择类别' }],initialValue: defaultCategory}]">
            <a-select-option v-for="category in categoryList" :key="category.id">{{category.name}}
            </a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="商户名称">
          <a-input placeholder="商户名称" v-decorator="[ 'name', {rules: [{ required: true, message: '请输入商户名称' }]}]"
          ></a-input>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="云支付门店id">
          <a-input placeholder="云支付门店id" v-decorator="[ 'outShopId', {rules: [{ required: true, message: '请输入云支付门店id' }]}]"
          ></a-input>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="营业执照名称">
          <a-input placeholder="营业执照名称" v-decorator="[ 'licenseName', {rules: [{ required: true, message: '请输入营业执照名称' }]}]"
          ></a-input>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="商户地址">
          <a-textarea :rows="2" placeholder="商户地址" v-decorator="['address', {rules: [{ required: true,message: '请输入商户地址' }]}]"/>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="联系人">
          <a-input placeholder="联系人" v-decorator="['contact', {rules: [{ required: true,message:'请输入联系人' }]}]"/>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="手机号">
          <a-input placeholder="手机号" v-decorator="['mobile', {rules: [{ required: true ,message: '请输入手机号'}]}]"/>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="座机号">
          <a-input placeholder="座机号" v-decorator="['telephone']"/>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { save } from '../../../api/business/merchant'
  import { selectAllCompany } from '../../../api/system/company'
  import { selectAllCategory } from '../../../api/business/category'
  import pick from 'lodash.pick'

  export default {
    //--############  data() ###############-->
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        visible: false,
        confirmLoading: false,
        mdl: {},
        form: this.$form.createForm(this),

        isDisabled: false,

        defaultCompany: '',
        defaultCategory: '',
        companyList: [],
        categoryList: []

      }
    },
    //--############  data() ###############-->

    created() {
      this.getAllCompany()
    },


    //--############  methods() ###############-->
    methods: {
      getAllCompany: function() {
        selectAllCompany().then(res => {
          if (res.code === 200) {
            this.companyList = res.result
            this.defaultCompany = this.companyList[0].id
            this.getAllCategory(this.defaultCompany)
          } else {
            this.$notification.error({
              message: '错误',
              description: res.message,
              duration: 4
            })
          }
        })
      },
      getAllCategory: function(companyId) {
        selectAllCategory(companyId).then(res => {
          if (res.code === 200) {
            this.categoryList = res.result
            this.defaultCategory = this.categoryList[0].id
          } else {
            this.$notification.error({
              message: '错误',
              description: res.message,
              duration: 4
            })
          }
        })
      },
      add() {
        this.isDisabled = false
        this.visible = true
      },
      edit(record) {
        this.isDisabled = true
        this.mdl = Object.assign({}, record)
        this.visible = true
        // this.defaultCompany = this.mdl.companyId
        this.getAllCategory(this.mdl.companyId)
        // this.defaultCategory = this.mdl.categoryId
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.mdl, 'name','licenseName', 'companyId','address', 'contact', 'mobile', 'telephone', 'categoryId','outShopId'))
        })
      },
      close() {
        this.$emit('close')
        this.visible = false
        this.form.resetFields()
        this.mdl = {}
      },
      handleOk() {
        const _this = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          // 验证表单没错误
          if (!err) {
            values.id = this.mdl.id
            _this.confirmLoading = true
            // 模拟后端请求 2000 毫秒延迟
            save(values).then((res) => {
              if (res.code === 200) {
                _this.$message.success('保存成功')
                _this.$emit('ok')
                _this.close()
              } else {
                this.$notification.error({
                  message: '错误',
                  description: res.message,
                  duration: 4
                })
              }
            }).catch(() => {
              // Do something
            }).finally(() => {
              _this.confirmLoading = false
            })
          }
        })
      },
      handleCancel() {
        this.close()
      },
      handleProvinceChange(value) {
        this.getAllCategory(value)
      }
    }
    //--############  methods() ###############-->
  }
</script>

<style scoped>

</style>
